package beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;

@ManagedBean(name="pubBean")
public class PublicationYearBean {
	private String professor;
	private int year;
	private List<Prof> profs = new ArrayList<Prof>();

	public PublicationYearBean() {
		professor = (String) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap()
				.get("professorSelecionado");
		year = Integer.parseInt(((String) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("anoSelecionado")));
		String serviceURI = "http://grupo03.mooo.com/grupo03/query";
		String queryString = "PREFIX grupo03:<http://www.owl-ontologies.com/Ontology1307822014.owl#> ";
		queryString+="prefix foaf: <http://xmlns.com/foaf/0.1/> ";
		queryString+="prefix authors: <http://dblp.l3s.de/d2r/resource/authors/> ";
		queryString+="prefix dc: <http://purl.org/dc/elements/1.1/> ";
		queryString+="prefix dcterms: <http://purl.org/dc/terms/> ";
		queryString+="prefix xsd: <http://www.w3.org/2001/XMLSchema#> ";
		queryString+="SELECT ?t ?z ";
		queryString+="WHERE{ ";
		queryString+="?prof grupo03:nome ?t. ";
		queryString+="?prof grupo03:autor ?autor ";
		queryString+="SERVICE<http://dblp.l3s.de/d2r/sparql>{ ";
		queryString+="?a foaf:maker ?autor; ";
		queryString+="dc:title ?z; ";
		queryString+="dcterms:issued \"2010\"^^xsd:gYear. ";
		queryString+="} ";
		queryString+="} ";
		
		Query query = QueryFactory.create(queryString);

		QueryExecution qe = QueryExecutionFactory.sparqlService(serviceURI,
				query);
		ResultSet results = qe.execSelect();
		while (results.hasNext()) {
			QuerySolution solution = results.next();
			Iterator<String> iter = solution.varNames();
			profs.add(new Prof(solution.getLiteral("t").getString(), solution.getLiteral("z").getString(), ""));
		}

	}

	public String getProfessor() {
		return professor;
	}

	public void setProfessor(String professor) {
		this.professor = professor;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public List<Prof> getProfs() {
		return profs;
	}

	public void setProfs(List<Prof> profs) {
		this.profs = profs;
	}
	
	

}
